package org.paradise.dms.util;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.paradise.dms.util.jsonbean.*;

import net.sf.json.JSONObject;

import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.apache.log4j.Logger;
import org.paradise.dms.util.jsonbean.PermissionValue;

/*
 * 用过滤器验证登陆,看session是否有效
 */
public class SessionFilter implements Filter {
	private static final Logger log = Logger.getLogger(SessionFilter.class);
	FilterConfig fc;

	public void destroy() {
		// 消毁方法
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest,
	 *      javax.servlet.ServletResponse, javax.servlet.FilterChain) 验证Session
	 */
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest hreq = (HttpServletRequest) request;
		HttpServletResponse hres = (HttpServletResponse) response;
		HttpSession session = hreq.getSession();
		String uriStr = hreq.getRequestURI();
		
		String urlwrong = hreq.getContextPath() + "/urlPrevent.jsp";
		//如果是请求登录页则放行
		//System.out.println("#####"+uriStr);
	
		if(uriStr.indexOf("urlPrevent.jsp") > -1 || uriStr.indexOf("login.jsp") > -1 || uriStr.indexOf("login.action") > -1){
			//System.out.println("s");
			chain.doFilter(request, response);
		//新生注册或登录系统放行
		}else if (uriStr.indexOf("studentlogin")>-1 || uriStr.indexOf("active.jsp") > -1){
			chain.doFilter(request, response);
		}
		else if (null == session){
			hres.sendRedirect(hreq.getContextPath()+ "/login.jsp");
		}else if (session != null && null != session.getAttribute("studentno")) {
			if(uriStr.indexOf("studentreport")>-1) {
				chain.doFilter(request, response);
			}
		}else if (session != null && null != session.getAttribute("userpermissionvalue")) {
			JSONObject json = (JSONObject)session.getAttribute("userpermissionvalue");
			
			PermissionValue pv = (PermissionValue)JSONObject.toBean(json, PermissionValue.class); 
			//System.out.println("upv =====" +ReflectionToStringBuilder.toString(pv).toString());
			
			
			
			if(!"1".equals(pv.getS1())){
				hres.sendRedirect(urlwrong);
			}
			
			if((uriStr.indexOf("aptmgr/aptlist.jsp") > -1) ||(uriStr.indexOf("aptmgr/aptcreation.jsp") > -1) ||(uriStr.indexOf("aptmgr/dormlist.jsp") > -1) ||(uriStr.indexOf("aptmgr/dormbatchupdate.jsp") > -1) ||(uriStr.indexOf("aptmgr/dormrating.jsp") > -1)){
				if(!"1".equals(pv.getS2())){
					hres.sendRedirect(urlwrong);
				}
			}
			
			else if(uriStr.indexOf("student/basicSearchstudent.jsp") > -1 || uriStr.indexOf("student/searchstudentbyconditions.jsp") > -1){
				if(!"1".equals(pv.getS3())){
					hres.sendRedirect(urlwrong);
				}
			}
			
			else if(uriStr.indexOf("student/addstudent.jsp") > -1 || uriStr.indexOf("student/uploadStudentInfo.jsp") > -1 || uriStr.indexOf("student/collegeAndMajor.jsp") > -1){
				if(!"1".equals(pv.getS4())){
					hres.sendRedirect(urlwrong);
				}
			}
			
			else if(uriStr.indexOf("lodgemgr/allocateoneperson.jsp") > -1 || uriStr.indexOf("lodgemgr/timelyallocation.jsp") > -1 || uriStr.indexOf("lodgemgr/batchallocate.jsp") > -1){
				if(!"1".equals(pv.getS5())){	
					hres.sendRedirect(urlwrong);
				}
			}
			else if (uriStr.indexOf("lodgemgr/exchangeroom.jsp") > -1 || uriStr.indexOf("lodgemgr/changetonewroom.jsp") > -1 ){
				if(!"1".equals(pv.getS6())){	
					hres.sendRedirect(urlwrong);
				}
			}
			else if (uriStr.indexOf("lodgemgr/singlecheckout.jsp") > -1 || uriStr.indexOf("lodgemgr/batchcheckout.jsp") > -1 ){
				if(!"1".equals(pv.getS7())){	
					hres.sendRedirect(urlwrong);
				}
			}
			else if (uriStr.indexOf("lodgemgr/lodgetimemgr.jsp") > -1  ){
				if(!"1".equals(pv.getS8())){	
					hres.sendRedirect(urlwrong);
				}
			}
			else if (uriStr.indexOf("maintainreport/maintainreportadd.jsp") > -1 || uriStr.indexOf("maintainreport/listAllMaintainReport.action") > -1 ){
				if(!"1".equals(pv.getS10())){	
					hres.sendRedirect(urlwrong);
				}
			}
			else if (uriStr.indexOf("systemannouncement/addsystemannouncement.jsp") > -1 || uriStr.indexOf("systemannouncement/listallsystemannouncement.action") > -1 ){
				if(!"1".equals(pv.getS11())){	
					hres.sendRedirect(urlwrong);
				}
			}
			else if (uriStr.indexOf("batchoperation/batchUpdate.jsp") > -1 ){
				if(!"1".equals(pv.getS12())){	
					hres.sendRedirect(urlwrong);
				}
			}
			else if (uriStr.indexOf("lodgefee/lodgefeemgr.jsp") > -1 ){
				if(!"1".equals(pv.getS13())){	
					hres.sendRedirect(urlwrong);
				}
			}
			else if (uriStr.indexOf("registerinfo/") > -1 ){
				if(!"1".equals(pv.getS14())){	
					hres.sendRedirect(urlwrong);
				}
			}
			else if (uriStr.indexOf("systemusergrouptaskallocation/") > -1 ){
				if(!"1".equals(pv.getS15())){	
					hres.sendRedirect(urlwrong);
				}
			}
			else if (uriStr.indexOf("systemusermgr/listsystemusergroup.jsp") > -1 || uriStr.indexOf("systemusermgr/systemusergroupmgr.jsp") > -1 || uriStr.indexOf("systemusermgr/systemusergrouprolemgr.jsp") > -1){
				if(!"1".equals(pv.getS16())){	
					hres.sendRedirect(urlwrong);
				}
			}
			else if (uriStr.indexOf("systemusermgr/addsystemuser.jsp") > -1 || uriStr.indexOf("systemusermgr/systemusermgr.jsp") > -1){
				if(!"1".equals(pv.getS17())){	
					hres.sendRedirect(urlwrong);
				}
			}
			else if (uriStr.indexOf("sysmgr/databasebackup.jsp") > -1 || uriStr.indexOf("sysmgr/preassigntypemgrindex.jsp") > -1){
				if(!"1".equals(pv.getS18())){	
					hres.sendRedirect(urlwrong); 
				}
			}
			else if (uriStr.indexOf("/report/") > -1 ){
				if(!"1".equals(pv.getS19())){	
					hres.sendRedirect(urlwrong);
				}
			}
			chain.doFilter(request, response);
		} else {
			hres.sendRedirect( hreq.getContextPath() + "/login.jsp");
			chain.doFilter(request, response);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.Filter#init(javax.servlet.FilterConfig) 初使化过滤器
	 */
	public void init(FilterConfig fc) throws ServletException {
		this.fc = fc;
	}

}